/**
 * 
 */
package edu.android;

import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;

/**
 * @author fabiomelillo
 *
 */
public class MoviesDB {
	
	private SQLiteDatabase mdb;
	private Context context;
	private DbHelper mdbhelper;
	private static final String DB_NAME="moviesdb"; //nome del database
	private static final int DB_VERSION=1; //versione del database
	
	
	public MoviesDB(Context ctx){
		context=ctx;
		mdbhelper = new DbHelper(ctx, DB_NAME, null, DB_VERSION);
	}
	
	public boolean open(){
		mdb=mdbhelper.getWritableDatabase();
		return (mdb==null) ? false : true;
	}
	
	public void close(){
		mdb.close();
	}
	
	public String getTableName(){
		return MovieMetaData.MOVIE_TABLE;
	}
	
	//inserimento
	/**
	 * @param movie
	 * @return -1 if error else the record id
	 */
	public Uri insertMovie(Movie movie){
		ContentValues cv=new ContentValues();
		cv.put(MovieMetaData.MOVIE_TITLE_KEY, movie.getTitle());
		cv.put(MovieMetaData.MOVIE_DIRECTOR_KEY, movie.getDirector());
		cv.put(MovieMetaData.MOVIE_YEAR_KEY, movie.getYear());
		cv.put(MovieMetaData.MOVIE_SEEN_KEY, movie.getSeen());
		cv.put(MovieMetaData.MOVIE_HD_KEY, movie.getHd());
		cv.put(MovieMetaData.MOVIE_RATE_KEY, movie.getRate());
		
		long id= mdb.insert(MovieMetaData.MOVIE_TABLE, null, cv);
		return Uri.parse(MovieMetaData.CONTENT_URI+"/"+id);
	}
	
	
	public Cursor fetchMovies(){ //metodo per fare la query di tutti i dati
        return mdb.query(MovieMetaData.MOVIE_TABLE, null,null,null,null,null,null);
		
	}
	
//	public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy){
//		return mdb.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
//	}
	
	public String[] getColumnNames(){
		String[] res=new String[7];
		res[0]=MovieMetaData.MOVIE_ID;
		res[1]=MovieMetaData.MOVIE_TITLE_KEY;
		res[2]=MovieMetaData.MOVIE_DIRECTOR_KEY;
		res[3]=MovieMetaData.MOVIE_YEAR_KEY;
		res[4]=MovieMetaData.MOVIE_SEEN_KEY;
		res[5]=MovieMetaData.MOVIE_HD_KEY;
		res[6]=MovieMetaData.MOVIE_RATE_KEY;
		return res;
	}
	
	
	
	
	
	private static final String MOVIES_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS "  //codice sql di creazione della tabella
            + MovieMetaData.MOVIE_TABLE + " (" 
            + MovieMetaData.MOVIE_ID+ " integer primary key autoincrement, "
            + MovieMetaData.MOVIE_TITLE_KEY + " text not null, "
            + MovieMetaData.MOVIE_DIRECTOR_KEY + " text, "
            + MovieMetaData.MOVIE_YEAR_KEY + " text, "
            + MovieMetaData.MOVIE_SEEN_KEY + " integer, "
            + MovieMetaData.MOVIE_HD_KEY + " integer, "
            + MovieMetaData.MOVIE_RATE_KEY + " integer);";
	
	
	private class DbHelper extends SQLiteOpenHelper{ //utility per la creazione del db
		
		
		
		
		public DbHelper(Context context, String name, CursorFactory factory,
				int version) {
			super(context, name, factory, version);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(MOVIES_TABLE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
			// TODO Auto-generated method stub
			
		}
		
	}


	public List<Movie> getMovies() {
		Cursor c=this.fetchMovies();
		return Movie.createMovieList(c);
	}
	

	

	public SQLiteDatabase getSQLiteDatabase() {
		
		return mdb;
	}

	public int deleteMovies(String where, String[] whereArgs) {
		int res= mdb.delete(MovieMetaData.MOVIE_TABLE, where, whereArgs);
		return res;
	}

	public int updateMovie(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		int res=mdb.update(MovieMetaData.MOVIE_TABLE, values, selection, selectionArgs);
		return res;
	}

	

}
